package com.group4.Servlets.mobile;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.group4.DAO.UserDAO;
import com.group4.DB.User;
import com.group4.Utility.ActivationKeyGenerator;
import com.group4.Utility.UserValidator;

/**
 * Servlet implementation class RegisterServlet
 */
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisterServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json");
		// Get the printwriter object from response to write the required json
		// object to the output stream
		
		
		String name = request.getParameter("name");
		String surname = request.getParameter("surname");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String email = request.getParameter("email");

		PrintWriter out = response.getWriter();
		JsonObject jsonObject = new JsonObject();
		
		if(name==null || surname == null || username == null || password == null || email == null){
			jsonObject.addProperty("error", "missing parameter!");
			out.print(jsonObject);
			out.flush();
		}else if(!User.userExist(username)){
			UserDAO newUser = new UserDAO();
			newUser.setName(name);
			newUser.setSurname(surname);
			newUser.setUsername(username);
			newUser.setPassword(password);
			newUser.setEmail(email);
			newUser.setActivationKey(ActivationKeyGenerator.createKey());
			User.addUser(newUser);
			Gson gson = new Gson();
            String json = gson.toJson(newUser);
            out.print(json);
			out.flush();
		}
		else{
			jsonObject.addProperty("error", "username is already being used");
			out.print(jsonObject);
			out.flush();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		response.setContentType("application/json");
		// Get the printwriter object from response to write the required json
		// object to the output stream
		
		
		String name = request.getParameter("name");
		String surname = request.getParameter("surname");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String email = request.getParameter("email");

		PrintWriter out = response.getWriter();
		JsonObject jsonObject = new JsonObject();
		
		if(name==null || surname == null || username == null || password == null || email == null){
			jsonObject.addProperty("error", "missing paramter!");
			out.print(jsonObject);
			out.flush();
		}else if(!User.userExist(username)){
			UserDAO newUser = new UserDAO();
			newUser.setName(name);
			newUser.setSurname(surname);
			newUser.setUsername(username);
			newUser.setPassword(password);
			newUser.setEmail(email);
			newUser.setActivationKey(ActivationKeyGenerator.createKey());
			User.addUser(newUser);
			Gson gson = new Gson();
            String json = gson.toJson(newUser);
            out.print(json);
			out.flush();
		}
		else{
			jsonObject.addProperty("error", "username is already being used");
			out.print(jsonObject);
			out.flush();
		}
		
		
	}

}
